package com.mc.lp.view.controllers;

import javax.annotation.Resource;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mc.lp.database.service.handler.constant.FormHandler;
import com.mc.lp.database.service.handler.constant.FormRequestType;
import com.mc.lp.service.UserService;
import com.mc.lp.shared.vo.IValueObject;
import com.mc.lp.shared.vo.SearchCriteriaVO;
import com.mc.lp.shared.vo.UserVO;

/**
 * @author: deepak.verma
 * @description: class to act as controller for user or parent section pages.
 * */
@Controller
public class UserViewController extends AbstractController<UserViewController>{

	@Resource
	UserService userService;

	@RequestMapping(value = PAGES.USER_WELCOME, method = RequestMethod.GET)
	public ModelAndView userWelcomePage() {
		UserVO userDetails = (UserVO)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ModelAndView model = new ModelAndView();
		model.addObject("username", userDetails.getUsername());
		model.addObject("userId", userDetails.getUserId());
		model.addObject("firstName", userDetails.getFirstName()); 
		model.addObject("lastName", userDetails.getLastName());
		model.setViewName(PAGES.USER_WELCOME);
		return model;
	}

	@RequestMapping(value = PAGES.FORGOT_CREDENTIALS_PAGE, method = RequestMethod.GET)
	public ModelAndView forgotCredentials() throws Exception {
		
		if(true){
			throw new Exception("This is test exception");
		}
		ModelAndView model = new ModelAndView();
		model.addObject("title", "Welcome To Microcampus");
		model.addObject("message", "This is protected page!");
		model.setViewName("redirect:" + PAGES.USER_WELCOME);
		return model;
	}
	
	@ResponseBody
	@RequestMapping(value="/user/handleRequest/{formRequestType}", method=RequestMethod.POST,headers="Accept=application/json")
	public IValueObject studentRecordRequestHandler(@RequestBody SearchCriteriaVO searchCriteria, @PathVariable String formRequestType) throws Exception{
		return handleRequestFromUI(FormHandler.USER_HANDLER,FormRequestType.valueOf(formRequestType),searchCriteria);
	}
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
